import { _decorator, Component, Label, Node } from 'cc';
import { TextContent } from './TextContent';
import { CodeContent } from './CodeContent';
import { Settlement } from './Settlement';
import { ProgressTimer } from './ProgressTimer';
import { GameStorage, GameTimer, StorageKey } from './Global';
import { GameData } from './GameData';
import { LevelLabel } from './LevelLabel';
import { SearchCount } from './SearchCount';
const { ccclass, property } = _decorator;

// 这个挂载到bg上，主要是重启游戏
// 这个主要是初始化游戏
@ccclass('Main')
export class Main extends Component {

    @property({ type: ProgressTimer })
    progressTimer: ProgressTimer = null;

    @property({ type: CodeContent })
    codeLibraryContent: CodeContent = null;

    @property({ type: TextContent })
    titleTextContent: TextContent = null;

    @property({ type: Settlement })
    settlement: Settlement = null;

    @property({ type: LevelLabel })
    levelLabel: LevelLabel = null;

    @property({ type: SearchCount })
    searchCount: SearchCount = null;

    start() {
        // 初始化关卡为1
        let level = GameStorage.getItem(StorageKey.LEVEL) || 1;
        GameData.initLevelData(level);

    }

    update(deltaTime: number) {

    }

    // 重启游戏，挂载在【两试一次】的buttom上的按钮事件
    restart() {

        // 重置关卡
        let level = GameStorage.getItem(StorageKey.LEVEL) || 1;
        GameData.initLevelData(level);

        // 重置时间进度条
        this.progressTimer.restart();
        // 重置题目
        this.titleTextContent.restart();
        // 重置字库
        this.codeLibraryContent.restart();
        // 重置结算
        this.settlement.restart();
        // 关卡显示
        this.levelLabel.restart();
        // 道具数量及使用次数
        this.searchCount.restart();
    }

    // 跳转到下一关
    gotoNextLevel() {
        // 关卡加1
        let level = GameStorage.getItem(StorageKey.LEVEL) || 1;
        level = parseInt(level) + 1;
        GameStorage.setItem(StorageKey.LEVEL, level);
        this.restart();
    }
}


